package com.lintcode.solution;

import java.util.ArrayList;
import java.util.List;

public class Solution_16 {
    public static void main(String[] args) {
        int[] nums = { 1, 2, 2 };
        Solution_16 solution = new Solution_16();
        System.out.println(solution.permute(nums));
    }
    
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            list.add(new ArrayList<>());
            return list;
        }
        if (nums.length == 1) {
            List<Integer> arr = new ArrayList<>();
            arr.add(nums[ 0 ]);
            list.add(arr);
            return list;
        }
        int[] temp;
        List<List<Integer>> tempList;
        for (int i = 0; i < nums.length; i++) {
            temp = new int[ nums.length - 1 ];
            for (int j = 0, k = 0; j < temp.length; j++, k++) {
                if (k == i) {
                    k++;
                }
                temp[ j ] = nums[ k ];
            }
            tempList = permute(temp);
            for (List<Integer> integers : tempList) {
                integers.add(0, nums[ i ]);
                list.add(integers);
            }
        }
        return list;
    }
    
    public List<List<Integer>> permute1(int[] nums) {
        int a = nums.length, size = 1;
        while (a > 0) {
            size *= a--;
        }
        List<List<Integer>> list = new ArrayList<>(size);
        for (int j = 1; j <= size; j++) {
            list.add(new ArrayList<>());
        }
        List<Integer> arr;
        int length = nums.length;
        for (int k = 0; k < length; k++) {
            for (int i = 0, j = 0; i < size; i++) {
                arr = list.get(i);
                
            }
        }
        return list;
    }
}
